#include "stdafx.h"
#include "Petals.h"

Interface_SplashScreen_Effect_Petals :: Interface_SplashScreen_Effect_Petals()
:mTimepassed(0.0f)
{
}

Interface_SplashScreen_Effect_Petals :: ~Interface_SplashScreen_Effect_Petals()
{
	mPetalsImg.release();

	mHandler.reset(NULL);
	mHandler.release();
}

bool Interface_SplashScreen_Effect_Petals :: Initialize()
{
	float Interval = APP_WIDTH / 16.0f;
	for(size_t j = 1; j < 4; j++)
	{
		for(size_t i = 0; i < 16; i++)
			mSpawnPoints.push_back(Vector2D(- APP_WIDTH * 0.7f + i * Interval, APP_HEIGHT * 0.1f * j));
	}
	mHandler = SmartPtr<ParticleSystem>(new ParticleSystem(30));

	mPetalsImg = SmartPtr<hgeSprite>(g_pSystem_Main->RESOURCE()->GetSprite("SplashScreen_EffectPetals"));
	mPetalsImg->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
	return true;
}

void Interface_SplashScreen_Effect_Petals :: Update(const float & Interval)
{
	mTimepassed += Interval;
	if(mTimepassed > 0.24f && mHandler->ActiveCount() < 20)
	{
		mHandler->Emit(1, mSpawnPoints[rand() % mSpawnPoints.size()]);
		mTimepassed = 0.0f;
	}
	mHandler->Update(Interval);
}

void Interface_SplashScreen_Effect_Petals :: Render()
{
	g_pSystem_Main->HGESYSTEM()->System_SetState(HGE_TEXTUREFILTER , false);
	std :: deque <Graphic_Particle> * ReferenceList = mHandler->GetParticles();
	Graphic_Particle * Ref = NULL;

	for(int i = static_cast<int>(ReferenceList->size()) - 1; i != -1; i--)
	{
		Ref = &ReferenceList->at(i);
		if(Ref->mRespawn || Ref->mLife <= 0.0f) continue;

		mPetalsImg->SetColor(Ref->mColor);
		mPetalsImg->RenderEx(Ref->mPosition.x, Ref->mPosition.y, Ref->mBearing, Ref->mScale.x, Ref->mScale.y);
	}
	g_pSystem_Main->HGESYSTEM()->System_SetState(HGE_TEXTUREFILTER , true);
	Ref = NULL;
	ReferenceList = NULL;
}